const reg = (() => {
    $.validator.addMethod("checkName", function (val, ele) {
        return /^[a-z]+\w+/i.test(val);
    })

    axios.defaults.baseURL = "";

    axios.interceptors.request.use(function (config) {
        return config;
    })
    axios.interceptors.response.use(function (config) {
        return config;
    })

    const validate = () => {
        $("form").validate({
            rules: {
                uname:{
                    required: true,
                    rangelength: [6, 18],
                    checkName: true
                },
                upwd: {
                    required: true
                }
            },
            messages: {
                uname: {
                    required: "用户必填",
                    rangelength: "用户名长度必须是{0}-{1}",
                    checkName: "用户名必须字母开头"
                },
                upwd: {
                    required: "密码必填"
                }
            },
            submitHandler() {
                let data = new URLSearchParams();
                data.append("name", $('[name="uname"]').val());
                data.append("pwd", $('[name="upwd"]').val());
                axios({
                    url: "/account/login",
                    method: "post",
                    data: data
                }).then(function (res) {
                    layer.msg(res.data.msg)
                    if(res.data.code == 200){
                        localStorage.setItem("token",res.data.token);
                        localStorage.setItem("user",JSON.stringify(res.data.data));
                        setTimeout(() => {
                            window.open("./");
                        }, 2000);
                    }
                })
                return false;
            }
        })
    }
    return {
        validate
    }
})()

$(function () {
    reg.validate();
})

